package activemq.jms.topic;

import java.time.LocalDateTime;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class Producer {

	private static final String URL = "tcp://127.0.0.1:61616";

	private static final String TNAME = "T-text";

	public static void main(String[] args) throws JMSException, InterruptedException {
		// 1、创建连接工厂
		ConnectionFactory factory = new ActiveMQConnectionFactory(URL);

		// 2、创建连接
		Connection conn = factory.createConnection();

		// 3、启动连接
		conn.start();

		// 4、创建回话
		Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

		// 5、创建目的地
		Destination destination = session.createTopic(TNAME);

		// 6、创建生产者
		MessageProducer producer = session.createProducer(destination);
		for (int i = 0; i < 100; i++) {
			// 7、创建消息
			TextMessage message = session.createTextMessage();
			message.setText("test :" + i + " time=" + LocalDateTime.now());
			// 8、发送消息
			producer.send(message);
			Thread.sleep(1000);
			System.out.println("发送消息 ：" + message.getText());
		}

		Thread.sleep(10000);
		conn.close();

		System.out.println("关闭连接");
	}

}
